//
//  MBOrderTypeView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBOrderTypeView.h"

@interface MBOrderTypeView ()


@end

@implementation MBOrderTypeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
        gradientLayer0.frame = CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:44]);
        gradientLayer0.colors = @[
                                  (id)[UIColor colorWithRed:255.0f/255.0f green:136.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                                  (id)[UIColor colorWithRed:255.0f/255.0f green:99.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
        gradientLayer0.locations = @[@0, @1];
        [gradientLayer0 setStartPoint:CGPointMake(0, 0)];
        [gradientLayer0 setEndPoint:CGPointMake(1, 0)];
        [self.layer addSublayer:gradientLayer0];
        
        NSArray* titleArray = @[@"全部订单",@"订单价格",@"订单时间"];
        CGFloat width = (SCREEN_WIDTH-[UIView lf_sizeFromIphone6:30])/3;
        for (int i=0; i<titleArray.count; i++) {
            UIButton* button = [[UIButton alloc]init];
            button.tag = i;
            [button addTarget:self action:@selector(handleClickButton:) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:titleArray[i] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont lf_systemFontOfSize:15];
            if (i != 0) {
                [button setImage:[UIImage imageNamed:@"choose_un"] forState:UIControlStateNormal];
            }else {
                [button setImage:[UIImage imageNamed:@"day_un_choose"] forState:UIControlStateNormal];
            }
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -[UIView lf_sizeFromIphone6:10], 0, 0);
            button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.intrinsicContentSize.width+[UIView lf_sizeFromIphone6:30], 0, 0);
            [button setTitleColor:[UIColor colorWithR:120 G:120 B:120] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [self addSubview:button];
            
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.left.equalTo(self).offset([UIView lf_sizeFromIphone6:15]+width*i);
                make.width.mas_equalTo(width);
                make.height.mas_equalTo([UIView lf_sizeFromIphone6:44]);
            }];
            
            if (i == 0) {
                button.selected = YES;
                self.selectButton = button;
            }

            if (i != titleArray.count -1) {
                UIView* line = [[UIView alloc]init];
                line.backgroundColor = [UIColor whiteColor];
                [self addSubview:line];
                
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.centerY.equalTo(button);
                    make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:1], [UIView lf_sizeFromIphone6:14]));
                }];
            }

        }
    }
    return self;
}

-(void)handleClickButton:(UIButton*)button
{
    if (button.tag == 0) {
//        全部
        if (self.orderType == -2 || self.orderType == -1) {
            if (self.orderType == -2) {
                [button setImage:[UIImage imageNamed:@"day_choose"] forState:UIControlStateSelected];
                self.orderType = -1;
            }else {
                [button setImage:[UIImage imageNamed:@"day_un_choose"] forState:UIControlStateSelected];
                self.orderType = -2;
            }

        }else {
            self.selectButton.selected = NO;
            self.selectButton = button;
            [button setImage:[UIImage imageNamed:@"day_choose"] forState:UIControlStateSelected];
            self.orderType = -1;
            button.selected = YES;
        }
    }else if (button.tag == 1){
//        价格
        if (self.orderType == 1 || self.orderType == 2) {
            if (self.orderType == 1) {
                [button setImage:[UIImage imageNamed:@"choose_bottom"] forState:UIControlStateSelected];
                self.orderType = 2;
            }else {
                [button setImage:[UIImage imageNamed:@"choose_top"] forState:UIControlStateSelected];
                self.orderType = 1;
            }
        }else {
            self.selectButton.selected = NO;
            self.selectButton = button;
            [button setImage:[UIImage imageNamed:@"choose_top"] forState:UIControlStateSelected];
            self.orderType = 1;
            button.selected = YES;
        }
    }else {
//        时间
        if (self.orderType == 3 || self.orderType == 4) {
            if (self.orderType == 3) {
                [button setImage:[UIImage imageNamed:@"choose_bottom"] forState:UIControlStateSelected];
                self.orderType = 4;
            }else {
                [button setImage:[UIImage imageNamed:@"choose_top"] forState:UIControlStateSelected];
                self.orderType = 3;
            }
        }else {
            self.selectButton.selected = NO;
            self.selectButton = button;
            [button setImage:[UIImage imageNamed:@"choose_top"] forState:UIControlStateSelected];
            self.orderType = 3;
            button.selected = YES;
        }
    }
}

@end
